// 添加-编辑-详情

<template>
  <view class="add">
    <mes-form ref="mesFormRef" :mesForm="mesForm"></mes-form>
    <mes-button :type="mesButton.type" :form="mesButton.form"></mes-button>
  </view>
</template>

<script>
export default {
  onLoad(options) {
    this.options = options
    getApp().globalData.goods = {}
  },
  onShow() {
    this[this.options.type]()
    // 选择的商品
    this.mesForm.row.goods_code = getApp().globalData.goods.goods_code
    this.mesForm.row.goods_name = getApp().globalData.goods.goods_name
  },
  data() {
    return {
      goods: {},
      options: null,
      mesButton: {
        type: '',
        form: null
      },
      mesForm: {
        row: {
          purchase_code: '',
          name: '',
          goods_code: '',
          goods_name: '',
          wages: ''
        },
        items: [
          {
            title: '清单编号',
            slots: {
              field: 'purchase_code',
              type: 'input'
            },
            rules: [{ type: 'required', message: '请输入清单编号' }]
          },
          {
            title: '清单名称',
            slots: {
              field: 'name',
              type: 'input'
            },
            rules: [{ type: 'required', message: '请输入清单名称' }]
          },
          {
            title: '商品名称',
            slots: {
              field: 'goods_name',
              icon: 'arrow',
              placeholder: '请选择'
            },
            rules: [{ type: 'required', message: '请选择商品' }],
            tap: () => {
              uni.navigateTo({ url: '/pages/common/goods?type=bom_list' })
            }
          },
          {
            title: '计件工资',
            slots: {
              field: 'wages',
              type: 'input',
              input_type: 'digit',
              digit_type: 'price'
            }
          }
        ]
      }
    }
  },
  methods: {
    // 添加
    async add() {
      const code = await this.$tools.getCode(29)
      this.mesForm.row.purchase_code = code || ''

      this.mesButton.type = 3
      this.mesButton.form = {
        leftText: '重置',
        rightText: '确定',
        leftEvent: () => {
          getApp().globalData.goods = {}
          this.mesForm.row = {
            purchase_code: code,
            name: '',
            goods_code: '',
            goods_name: '',
            wages: ''
          }
        },
        rightEvent: () => {
          this.$refs.mesFormRef.validate(async (valid) => {
            if (!valid) return
            const res = await this.$api.prod.bom.add(this.mesForm.row)
            if (res.code == 200) {
              uni.showToast({
                title: res.msg,
                icon: 'none',
                mask: true
              })
              this.$tools.back()
            }
          })
        }
      }
    },
    // 编辑
    async edit() {
      const res = await this.$api.prod.bom.getdetail({ id: this.options.id })
      if (res.code != 200) return

      Object.assign(this.mesForm.row, res.data)
      const detail = JSON.parse(JSON.stringify(res.data))

      getApp().globalData.goods = {
        goods_code: detail.goods_code,
        goods_name: detail.goods_name
      }

      this.mesButton.type = 3
      this.mesButton.form = {
        leftText: '重置',
        rightText: '保存',
        leftEvent: () => {
          Object.assign(this.mesForm.row, detail)
          getApp().globalData.goods = {
            goods_code: detail.goods_code,
            goods_name: detail.goods_name
          }
        },
        rightEvent: () => {
          this.$refs.mesFormRef.validate(async (valid) => {
            if (!valid) return
            const res = await this.$api.prod.bom.edit(this.mesForm.row)
            if (res.code == 200) this.$tools.successBack(res.msg)
          })
        }
      }
    },
    // 详情
    async detail() {
      this.mesForm.items.map((item) => (item.slots.is_detail = true))

      const res = await this.$api.prod.bom.getdetail({ id: this.options.id })
      if (res.code != 200) return

      Object.assign(this.mesForm.row, res.data)

      getApp().globalData.goods = {
        goods_code: res.data.goods_code,
        goods_name: res.data.goods_name
      }

      this.mesButton.type = 4
      this.mesButton.form = {
        leftText: '删除',
        centerText: '编辑',
        rightText: '生产工序',
        leftEvent: () => {
          uni.showModal({
            title: '提示',
            content: '是否确认删除？',
            success: async ({ confirm }) => {
              if (confirm) {
                const res = await this.$api.prod.bom.del({
                  id: this.options.id
                })
                if (res.code == 200) this.$tools.successBack(res.msg)
              }
            }
          })
        },
        centerEvent: () => {
          uni.navigateTo({
            url: `/pages/home/bom_list/add?type=edit&id=${this.options.id}`
          })
        },
        rightEvent: () => {
          uni.navigateTo({
            url: `/pages/home/bom_list/process?id=${this.options.id}`
          })
        }
      }
    }
  }
}
</script>
